package com.laundrySys.util;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;

/**
 * @author wufeng
 * @date 创建时间：2017年3月23日 下午3:59:28 
 */
public class JSONSerielizer {
	public static DateTransformer dateFormater=new DateTransformer("yyyy-MM-dd'T'HH:mm:ss");
	
	public static String toJson(Object obj,String[] includeStr,String[] excludeStr){	
		if(obj == null || obj.toString().equals("null")) return null;
		if(obj != null && obj.getClass() == String.class){
			return obj.toString();
		}
		JSONSerializer serializer = new JSONSerializer();
		if(includeStr!=null){
			serializer.include(includeStr);
		}
		if(excludeStr!=null){
			serializer.exclude(excludeStr);
		}
		serializer.transform(new DateTransformer("yyyy-MM-dd'T'HH:mm:ss"), Date.class);
		serializer.transform(new DateTransformer("yyyy-MM-dd'T'HH:mm:ss"), Timestamp.class);
		return serializer.deepSerialize(obj);

	}
	
	
	public static Map<String, Object> toMap(String json){
		Boolean canDo =  true;
		
		canDo = json.startsWith("{") && json.endsWith("}");
		if (!canDo) {
			return new HashMap<String, Object>();
		}
		JSONDeserializer<Map<String, Object>> jsonDeserializer = new JSONDeserializer<Map<String,Object>>();
		return jsonDeserializer.deserialize(json);
		
	}

	
	public static List<Map<String, Object>> toListMap(String json){
		Boolean canDo =  true;
		canDo = json.startsWith("{") && json.endsWith("}");
		if (!canDo) {
			return new ArrayList<Map<String,Object>>();
		}
		JSONDeserializer<List<Map<String, Object>>> jsonDeserializer = new JSONDeserializer<List<Map<String, Object>>>();
		return jsonDeserializer.deserialize(json);
	}
}
